package vn.info.vietnaminfo;

import java.net.URLEncoder;
import java.util.List;

import vn.info.vietnaminfo.adapter.RetailerAdapter;
import vn.info.vietnaminfo.data.DSDiaDiem;
import vn.info.vietnaminfo.dialog.ShowDialog;
import vn.info.vietnaminfo.sync.SyncListRetailer;
import vn.info.vietnaminfo.utli.GlobalVariable;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class RetailerActivity extends Activity
{
	public static Context mContext;
	private ImageButton btnClearText;
	private EditText txtSearch;
	private ListView listViewRetailer;
	private RetailerAdapter adapterRetail;
	private SyncListRetailer retailer;
	private RelativeLayout layoutTooltip;
	private LinearLayout linearTooltipProduct, linearTooltipDetail,linearTooltipEdit,linearTooltipDirection,linearTooltipDelete,linearTooltipClose;
	private int position = -1;
	private List<DSDiaDiem> _list;
	private final String TAG = "RetailerActivity";
	private final String FUNCTION ="HungCX---> ";
	private boolean isFirstTime = true;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_retailer);

		mContext 			= this;
		btnClearText 		= (ImageButton)findViewById(R.id.btnClearText);
		txtSearch 			= (EditText)findViewById(R.id.txtSearch);
		listViewRetailer	= (ListView)findViewById(R.id.listviewRetail);
		layoutTooltip		= (RelativeLayout)findViewById(R.id.layout_tooltip);

		layoutTooltip.setVisibility(View.GONE);
		// Tooltip

		linearTooltipProduct = (LinearLayout) findViewById(R.id.linear_layout_tooltip_product);
		linearTooltipProduct.setOnClickListener(clickListener);

		linearTooltipDetail 	= (LinearLayout)findViewById(R.id.linear_layout_tooltip_info);
		linearTooltipDetail.setOnClickListener(clickListener);

		linearTooltipEdit 	= (LinearLayout)findViewById(R.id.linear_layout_tooltip_edit);
		linearTooltipEdit.setOnClickListener(clickListener);

		linearTooltipDirection = (LinearLayout)findViewById(R.id.linear_layout_tooltip_direction);
		linearTooltipDirection.setOnClickListener(clickListener);

		linearTooltipDelete = (LinearLayout)findViewById(R.id.linear_layout_tooltip_delete);
		linearTooltipDelete.setOnClickListener(clickListener);
		
		linearTooltipClose = (LinearLayout)findViewById(R.id.linear_layout_tooltip_close);
		linearTooltipClose.setOnClickListener(clickListener);


		GlobalVariable.onPauseRetailer = true;


		listViewRetailer.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int _position,
					long arg3) {
				// TODO Auto-generated method stub
				position = _position;
				if(layoutTooltip.getVisibility() == View.GONE || layoutTooltip.getVisibility() == View.VISIBLE)
				{
					GlobalVariable.showAnimation(isFirstTime,mContext,_position ,layoutTooltip,adapterRetail.getCount());
					layoutTooltip.setVisibility(View.VISIBLE);
					Toast.makeText(mContext, "Đang chọn cửa hàng "+_list.get(_position).getTenDiaDiem(), Toast.LENGTH_SHORT).show();
				}

				InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				in.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);


				Log.v(TAG, FUNCTION + position);
			}
		});
		btnClearText.setOnClickListener(clickListener);
		btnClearText.setVisibility(View.GONE);



		txtSearch.addTextChangedListener(new TextWatcher()
		{
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
				layoutTooltip.setVisibility(View.GONE);
				if(txtSearch.getText().length() > 0)
				{

					btnClearText.setVisibility(View.VISIBLE);
				}
				else
				{

					btnClearText.setVisibility(View.GONE);
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			public void afterTextChanged(Editable s) {

				String text = txtSearch.getText().toString().toLowerCase();
				adapterRetail.filter(text);

			}
		});

		txtSearch.setOnEditorActionListener(new OnEditorActionListener()
		{
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
			{

				return true;
			}
		});

	}



	public void setAdapterRetailer(List<DSDiaDiem> list)
	{
		try
		{
			Log.v("SyncListRetailer", "HungCX--->list = "+list);
			_list = list;
			adapterRetail = new RetailerAdapter(mContext, list);
			listViewRetailer.setAdapter(adapterRetail);
		}catch(Exception e)
		{
			Toast.makeText(mContext, "Có lỗi xảy ra trong quá trình lấy dữ liệu. Xin hãy thử lại", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		GlobalVariable.onPauseRetailer = false;
	}

	protected void onResume()
	{
		super.onResume();
		layoutTooltip.setVisibility(View.GONE);
		if(GlobalVariable.onPauseRetailer)
		{
			retailer = new SyncListRetailer(mContext);
			retailer.execute();
		}
		
	}
	
	

	public void refreshRetailerActivity()
	{
		retailer = new SyncListRetailer(mContext);
		retailer.execute();
	}


	private View.OnClickListener clickListener = new View.OnClickListener()
	{
		public void onClick(View click)
		{
			if(btnClearText == click)
			{
				btnClearText.setVisibility(View.GONE);
				txtSearch.setText("");
			}
			else if(linearTooltipProduct == click)
			{
				
			}

			else if(linearTooltipDetail == click)
			{
				Intent intent = new Intent(mContext, PlaceDetailActivity.class);
				startActivityForResult(intent, 200);
				overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
			}

			else if (linearTooltipEdit == click)
			{
				
			}

			else if(linearTooltipDirection == click)
			{


			}
			else if(linearTooltipDelete == click)
			{
				

			}
			
			else if(linearTooltipClose == click)
			{
				//GlobalVariable.hideAnimation(isFirstTime,mContext,position ,layoutTooltip,adapterRetail.getCount());
				layoutTooltip.setVisibility(View.GONE);
				Toast.makeText(mContext, "Đã đóng menu", Toast.LENGTH_SHORT).show();

			}

		}
	};
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
	}

}